//#define __DEBUG__
// by Xeno
#define THIS_FILE "fn_makepatrolwpx.sqf"
#include "x_setup.sqf"

// supports also patrols in square areas, including angle
private ["_grp", "_start_pos", "_wp_array", "_i", "_wp_pos", "_counter", "_wp", "_wppos", "_pos", "_cur_pos","_no_pos_found", "_wpstatements", "_timeout", "_wp1"];
PARAMS_3(_grp,_start_pos,_wp_array);
__TRACE_3("","_grp","_start_pos","_wp_array");
// TYRGHEN - if we pass an object, we need to create a combo [center,radius]
if (typeName _wp_array == "OBJECT") then {_wp_array = [position _wp_array, 100]};
if (typeName _wp_array != "ARRAY") exitWith {};
if (typeName _start_pos == "OBJECT") then {_start_pos = position _start_pos};
if (typeName _start_pos != "ARRAY" || {count _start_pos == 0} || {isNull _grp}) exitWith {};
_timeout = if (count _this > 3) then {_this select 3} else {[]};
_wpstatements = if (count _this > 4) then {_this select 4} else {""};
_grp setBehaviour "CARELESS";
_grp setSpeedMode "FULL";
_cur_pos = _start_pos;
_no_pos_found = false;
deleteWaypoint [_grp, 0];
_wp = _grp addWaypoint [_start_pos, 0];
_wp setWaypointSpeed "FULL";
_wp setWaypointCompletionRadius (100 + random 100);
_grp setCurrentWaypoint _wp;
_grp setSpeedMode "FULL";
for "_i" from 0 to (2 + (floor (random 3))) do {
	_wp_pos = switch (count _wp_array) do {
		case 2: {[_wp_array select 0, _wp_array select 1] call FUNC(GetRanPointCircle)};
		case 4: {[_wp_array select 0, _wp_array select 1, _wp_array select 2, _wp_array select 3] call FUNC(GetRanPointSquare)};
	};
	if (count _wp_pos == 0) exitWith {_no_pos_found = true};
	_counter = 0;
	while {_wp_pos distance _cur_pos < ((_wp_array select 1)/6) && {_counter < 100}} do {
		_wp_pos = switch (count _wp_array) do {
			case 2: {[_wp_array select 0, _wp_array select 1] call FUNC(GetRanPointCircle)};
			case 4: {[_wp_array select 0, _wp_array select 1, _wp_array select 2, _wp_array select 3] call FUNC(GetRanPointSquare)};
		};
		if (count _wp_pos == 0) exitWith {};
		__INC(_counter);
	};
	if (count _wp_pos == 0) exitWith {_no_pos_found = true};
	_wp_pos = _wp_pos call FUNC(WorldBoundsCheck);
	_cur_pos = _wp_pos;
	_wp = _grp addWaypoint [_wp_pos, 0];
	_wp setWaypointType "MOVE";
	// TYRGHEN - Set completion radius at a high value so the group will stop to fight back instead of blindly walking
	_wp setWaypointCompletionRadius (100 + random 100);
	if (count _timeout > 0) then {
		_wp setWaypointTimeout _timeout;
	};
	
	if (_i == 0) then {
		_wp setWaypointSpeed "LIMITED";
		_wp setWaypointFormation "STAG COLUMN";
	};
	if (_wpstatements != "") then {
		_wp setWaypointStatements ["TRUE", _wpstatements];
	};
};
if (_no_pos_found) exitWith {
	_wp1 = _grp addWaypoint [_start_pos, 0];
	_wp1 setWaypointType "SAD";
};
_wp1 = _grp addWaypoint [_start_pos, 0];
_wp1 setWaypointType "MOVE";
// TYRGHEN - Set completion radius at a high value so the group will stop to fight back instead of blindly walking
_wp1 setWaypointCompletionRadius (100 + random 100);
if (count _timeout > 0) then {
	_wp1 setWaypointTimeout _timeout;
};
if (_wpstatements != "") then {
	_wp1 setWaypointStatements ["TRUE", _wpstatements];
};
_wp = _grp addWaypoint [_start_pos, 0];
_wp setWaypointType "CYCLE";
// TYRGHEN - Set completion radius at a high value so the group will stop to fight back instead of blindly walking
_wp setWaypointCompletionRadius (100 + random 100);
_grp setBehaviour "CARELESS";
_grp setSpeedMode "FULL";
#ifdef __DEBUG__
{ __TRACE_3("End","_grp","waypointPosition _x", "waypointType _x")
} foreach waypoints _grp;
#endif